﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace P2P_Backup.tools.io
{
    public class SimpleProgressArgs : EventArgs
    {
        public SimpleProgressArgs(long position)
        {
            this.position = position;
        }

        private long position;

        public long Position
        {
            get { return position; }
        }
    }
    public class StreamCopy
    {
        public event EventHandler<SimpleProgressArgs> Progessed;

        public void CopyStream(Stream input, Stream output,long length = -1)
        {
            try
            {
                byte[] buffer = new byte[32768];

                long position = 0;
                int read;
                while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
                {
                    output.Write(buffer, 0, read);
                    position += read;
                    onProgressed(position);
                }
            }
            catch(Exception e)
            {
                Console.WriteLine("Problem with file connection: {0}", e.Message);
                onProgressed(-1);
            }
        }
        private void onProgressed(long position)
        {
            if(Progessed != null) Progessed(this,new SimpleProgressArgs(position));
        }
    }
}
